package com.eunion.core;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

/**
 * 日期解析
 * 
 * 
 * 
 */
public class DateJsonProcessor implements JsonValueProcessor {

    public static final String Default_DATE_PATTERN = "yyyy-MM-dd";

    private DateFormat dateFormat;

    public DateJsonProcessor() {

        dateFormat = new SimpleDateFormat(Default_DATE_PATTERN);
    }

    public DateJsonProcessor(String datePattern) {
        try {
            dateFormat = new SimpleDateFormat(datePattern);
        } catch (Exception e) {
            dateFormat = new SimpleDateFormat(Default_DATE_PATTERN);
        }
    }

    private Object process(Object value) {
        if (value != null)
            return dateFormat.format((Date) value);
        else
            return null;
    }

    @Override
    public Object processArrayValue(Object value, JsonConfig jsonConfig) {
        return process(value);
    }

    @Override
    public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
        return process(value);
    }

}
